package com.shiroa.security;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Data
public class UserDetailslmpl implements UserDetails {
    private String username;
    private String password;
    private String nickName;
    private Set<String> rolesName;
    private Set<String> permsList;

    @Override
    @JsonDeserialize(using = CustomAuthorityDeserializer.class)
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<GrantedAuthority> list = new ArrayList<>();
        for (String roleName : rolesName) {
            SimpleGrantedAuthority authority = new SimpleGrantedAuthority("ROLE_"+roleName);
            list.add(authority);
        }
        for (String prmlist : permsList) {
            SimpleGrantedAuthority authority = new SimpleGrantedAuthority(prmlist);
            list.add(authority);
        }
//        list.addAll(rolesName.stream().map(rolesName ->new SimpleGrantedAuthority("ROLE_"+rolesName)).collect(Collectors.toList()));
//        list.addAll(permsList.stream().map(SimpleGrantedAuthority::new).collect(Collectors.toList()));
        return list;
    }

    @Override
    public String getPassword() {
        return password;
    }

    @Override
    public String getUsername() {
        return username;
    }

    @JsonIgnore
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @JsonIgnore
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @JsonIgnore
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @JsonIgnore
    @Override
    public boolean isEnabled() {
        return true;
    }
}
